<template>
  <div class="mainIndex">
    <n-form
      ref="formRef"
      :label-width="80"
      :model="formValue"
      :rules="rules"
    >
      <n-form-item label="账号" path="account">
        <n-input v-model:value="formValue.account" placeholder="电话号码" />
      </n-form-item>
      <n-form-item label="验证码" path="code">
        <n-input v-model:value="formValue.code" placeholder="验证码" />
      </n-form-item>
      <n-form-item>
        <n-button attr-type="button" @click="handleValidateClick">
          登录
        </n-button>
      </n-form-item>
    </n-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useMessage } from 'naive-ui'
import { code, login, currentUser, getDDCode } from "@/apis/login/index.js";
import { encrypt, decrypt } from '@/utils/jsencrypt' //rememberMe-password加密

const message = useMessage()
const formRef = ref()
const formValue = ref({
  account: "18326907519",
  password: "",
  origin: "code",
  code: "006688",
  timestamp: "",
  client_id: "admin",
  client_secret: "123456",
  scope: "all",
  grant_type: "password",
})
const rules = {
  account: {
    required: true,
    message: '请输入电话号码',
    trigger: ['input']
  },
  code: {
    required: true,
    message: '请输入验证码',
    trigger: ['input']
  }
}
const  handleValidateClick = (e) => {
  e.preventDefault()
  formRef.value?.validate( async (errors) => {
    if (!errors) {
      message.success('Valid')
      const res = await login(formValue.value);
      localStorage.setItem("token", res.token)
      await getCurrentUser()
    }
    else {
      console.log(errors)
      message.error('Invalid')
    }
  })
}
const getCurrentUser = async () => {
  const res = await currentUser();
  console.log(res)
  if (res) {

  }
}
</script>

<style scoped>

</style>
